
public class Table {
    private TableRow head;
    public Table(){
        head = null;
    }
    public int lookup(String key){
        TableRow tr = head;
        TableRow last = null;
        while(tr != null){
            if(tr.getId().compareTo(key) == 0){
                if(last != null){
                    last.setTail(tr.getTail());
                    tr.setTail(head);
                    head = tr;
                }
                return tr.getValue();
            }else{
                last = tr;
                tr = tr.getTail();
            }
        }
        if(tr == null)
            System.out.println("Error: key '"+key+"' not found");
        return 0;
    }
    public void update(String key, int v){
        TableRow tr = head;
        TableRow last = null;
        while(tr != null){
            if(tr.getId().compareTo(key) == 0){
                if(last != null){
                    last.setTail(tr.getTail());
                    tr.setTail(head);
                    head = tr;
                }
            }else{
                last = tr;
                tr = tr.getTail();
            }
        }
        if(tr == null){
            tr = new TableRow(key, v, head);
            head = tr;
        }
    }
}
